package com.teapos.service;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.teapos.dao.CustomerDao;
import com.teapos.dao.OrderDao;
import com.teapos.dao.UserDao;
import com.teapos.enumtype.BuyBackTypeEnum;
import com.teapos.enumtype.OrderStatusEnum;
import com.teapos.enumtype.PayStatusEnum;
import com.teapos.enumtype.PayTypeEnum;
import com.teapos.enumtype.SendStatusEnum;
import com.teapos.enumtype.TradeTypeEnum;
import com.teapos.enumtype.YNEnum;
import com.teapos.util.Lists;
import com.teapos.util.Maps;
import com.teapos.util.Tool;

@SuppressWarnings({"rawtypes", "unchecked"})
@Service
public class TradeService {
	
	@Autowired
	private UserDao userDao;
	@Autowired
	private OrderDao orderDao;
	@Autowired
	private CustomerDao customerDao;
	
	public List getStores() {
		return userDao.findAllStore();
	}
	
	public List getUserStores(int userId) {
		Map store = userDao.findStoreByUserId(userId);
		//System.out.println(store);
		int sort = Tool.toInt(store.get("sort"));
		//System.err.println(sort);
		if(sort==1) return userDao.findAllStore();
		else return Lists.newArrayList(store);
	}
	
	public List getOrders(Map param) {
		Tool.initPage(param);
		beforeQuery(param);
		List list = orderDao.findOrders(param);
		Iterator iter = list.iterator();
		int start = Tool.toInt(param.get("start"));
		while(iter.hasNext()) {
			Map map = (Map)iter.next();
			Maps.put(map, "has_save_display", YNEnum.getEnumByCode(Tool.toInt(map.get("has_save"))).getDesc(),
					"order_status_display", OrderStatusEnum.getEnumByCode(Tool.toInt(map.get("order_status"))).getDesc(),
					"index", ++start, "order_date_display", Tool.formatDate((Date)map.get("order_date"), "yyyy-MM-dd HH:mm"));
			//System.err.println(map);
			int type = Tool.toInt(param.get("type"));
			if(type==TradeTypeEnum.BUY.getCode()) {
				map.put("pay_type_display", PayTypeEnum.getEnumByCode(Tool.toInt(map.get("pay_type"))).getDesc());
				map.put("buyback_type_display", BuyBackTypeEnum.getEnumByCode(Tool.toInt(map.get("buyback_type"))).getDesc());
			} else if(type==TradeTypeEnum.BEFORE.getCode()) {
				map.put("pay_status_display", PayStatusEnum.getEnumByCode(Tool.toInt(map.get("pay_status"))).getDesc());
				map.put("send_status_display", SendStatusEnum.getEnumByCode(Tool.toInt(map.get("send_status"))).getDesc());
			}
		}
		return list;
	}
	
	public int getOrderCount(Map param) {
		beforeQuery(param);
		return orderDao.findOrderCount(param);
	}
	
	private void beforeQuery(Map param) {
		String orderNo = Tool.toString(param.get("order_no"));
		String customerName = Tool.toString(param.get("customer_name"));
		String customerTel = Tool.toString(param.get("customer_tel"));
		if(StringUtils.isNotBlank(orderNo)) Maps.put(param, "order_no", "%"+orderNo+"%");
		if(StringUtils.isNotBlank(customerName)) Maps.put(param, "customer_name", "%"+customerName+"%");
		if(StringUtils.isNotBlank(customerTel)) Maps.put(param, "telphone", "%"+customerTel+"%");
		//param.put("type", 1);
	}

	public Map getOrderDetails(String orderId, String customerId) {
		List products = orderDao.findOrderItems(Maps.newHashMap("order_id", orderId, "is_save", 0));
		List saves = orderDao.findOrderItems(Maps.newHashMap("order_id", orderId, "is_save", 1));
		Map customer = customerDao.findCustomerById(customerId);
		Map order = orderDao.findOrderById(orderId);
		return new Maps().put("products", products).put("saves", saves).put("customer", customer).
				put("order", order).build();
	}
	
	public Map getOrderAndDetail(String orderId, String customerId) {
		List products = orderDao.findOrderItems(Maps.newHashMap("order_id", orderId, "is_save", 0));
		Map order = orderDao.findOrderById(orderId);
		Map customer = customerDao.findCustomerById(customerId);
		return new Maps().put("products", products).put("order", order).put("customer", customer).build();
	}

}
